
 
import java.util.Vector;
 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
 

 public class JMTable extends CustomItem{
  private Vector colunas;
  private Vector linhas;
  private int largura;
  private int altura;
  private int indice;
  private int indice_marc;
  private int[] larg_columns;
  private Font fonte;
  private Font fonteCols;
 
  public JMTable(Vector colunas, Vector dados, int tam_fonte, int largura,int[] larg_columns)
  {
   super("");
   this.largura = largura;
   this.colunas = colunas;
   this.linhas = dados;
   this.larg_columns = larg_columns;
   fonte = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
     (tam_fonte==0 ? Font.SIZE_SMALL : tam_fonte == 1 ?
     Font.SIZE_MEDIUM:Font.SIZE_LARGE));
       
   //fonte = Font.getFont(Font.SIZE_SMALL);
   fonteCols = Font.getFont(Font.FACE_MONOSPACE,
     Font.STYLE_BOLD, (tam_fonte==0 ? Font.SIZE_SMALL : tam_fonte ==
      1 ? Font.SIZE_MEDIUM:Font.SIZE_LARGE));
   altura = fonte.getHeight()+(fonteCols.getHeight()*linhas.size())+(linhas.size()*2)+1;
  }
 
  protected int getMinContentHeight() {
   return fonte.getHeight();
  }
  protected int getMinContentWidth() {
   return largura;
  }
  protected int getPrefContentHeight(int param) {
   return altura;
  }
  protected int getPrefContentWidth(int param) {
   return largura;
  }
 
  protected void paint(Graphics g, int w, int h) {
   g.setColor(255, 255, 255);
   g.fillRect(0, 0, w-1, h-1);
 
   g.setColor(200, 200, 200);
   g.fillRect(0, 0, w, fonte.getHeight());
   g.setColor(0, 0, 0);
   g.drawLine(0, fonte.getHeight(), w, fonte.getHeight());
   for (int i = 1; i < colunas.size(); i++)
   {
    g.drawLine(larg_columns[i-1],0,larg_columns[i-1],h-1);
   }
   g.drawRect(0, 0, w-1, h-1);
   g.setStrokeStyle(g.DOTTED);
//   desenha linhas horizontais entre as linhas da tabela
   for (int i = 1; i < linhas.size(); i++)
   {
    g.drawLine(0, fonteCols.getHeight()+(fonte.getHeight()*(i))+2, w,(fonteCols.getHeight() + (fonte.getHeight()*(i)))+2);
   }
   g.setFont(fonteCols);
   for (int i = 1; i <= colunas.size(); i++)
   {
    String elem = (String)colunas.elementAt(i-1);
    if(i<2){
     g.drawString (colunas.elementAt( i-1 ).toString(),(larg_columns[i-1]-elem.length())/2,fonteCols.getHeight()-1, Graphics.HCENTER|Graphics.BASELINE);
    }else{
     g.drawString (colunas.elementAt( i-1 ).toString(),(larg_columns[i-2]+30),fonteCols.getHeight()-1, Graphics.HCENTER|Graphics.BASELINE);
    }
   }
   g.setFont(fonte);
   for (int i = 0; i < linhas.size(); i++)
   {
    if (i == indice){
     g.setColor(0, 0, 255);
        //linhaSelecionada = (String[])linhas.elementAt(i);
    }else{
     if (i == indice_marc){
     g.setColor(0, 0, 0);
     }else{
     g.setColor(0, 0, 0);
     }
    }
    String[] dados = (String[]) linhas.elementAt(i);
    for (int j = 1; j <= colunas.size(); j++)
    {
     String elem = dados[j-1];
     
     if(j<2){
      g.drawString (dados[j-1],(larg_columns[j-1]-elem.length())/2,fonteCols.getHeight() +(fonte.getHeight()*(i+1))+2, Graphics.LEFT|Graphics.BOTTOM);
     }else{
     g.drawString (dados[j-1],(larg_columns[j-2]+5),fonteCols.getHeight() +(fonte.getHeight()*(i+1))+2, Graphics.LEFT|Graphics.BOTTOM);
     }
     
    }
   }
  }
 
  protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
    int[] visRect_inout )
  {
   switch ( dir )
   {
   case Canvas.UP:
    return testaParaCima();
   case Canvas.DOWN:
    return testaParaBaixo();
   }
   return false;
  }
  public boolean testaParaCima(){
   if(indice>-1 && indice<linhas.size()){
    this.indice--;
    repaint();
   }else{
    this.indice = (linhas.size()-1);
    repaint();
   }
   return true;
  }
  public boolean testaParaBaixo(){
   if(indice>-1 && indice<linhas.size()){
    this.indice++;
    repaint();
   }else{
    this.indice=0;
    repaint();
   }
   return true;
  }
  public int getIndice(){
   return this.indice;
  }
}